package leetcode;

public class Lc7 {


    public static int reverse(int x) {
        int flag = 1;
        if (x < 0) {
            flag = -1;
            x *= -1;
        }
        char[] chars = new StringBuilder(String.valueOf(x)).reverse().toString().toCharArray();
        if (chars.length < 10) return flag * Integer.valueOf(new String(chars));

        char[] num = "2147483647".toCharArray();
        if (flag == -1) num = "2147483648".toCharArray();

        for (int i = 0; i < num.length; i++) {
            if (chars[i] != num[i]) {
                if (chars[i] > num[i]) return 0;
                else break;
            }

        }
        return flag * Integer.valueOf(new String(chars));
    }


    public static void main(String[] args) {
        StringBuilder stringBuilder = new StringBuilder(123);
        System.out.println(stringBuilder.toString());
        System.out.println(reverse(-2147483412));
    }
     //2147483412
    // 2147483647
    // 2143847412
    //-2147483648

}
